class TBNNCardLogic {
	public constructor() {
	}
	/**
	  * 最大7张
	  * @type {Number}
	  */
	public static maxcount = 5;
	/**
		* 花色掩码
		* @type {Number}
		*/
	public static maskColor = 0xF0;
	/**
		* 数值掩码
		* @type {Number}
		*/
	public static maskValue = 0x0F;
	/**
	  * 基础牌型
	  * @type {Array}
	  */
	public static cardDataArray =
	[
		0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x11, //梅花 2 - A
		0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, //方块 2 - A
		0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x21, //红桃 2 - A
		0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x31, //黑桃 2 - A
	]
	public static type =
	{
		/*** 无牛*/
		ox_value0: 0,
		/*** 牛牛*/
		ox_king: 10,
		/*** 四花牛*/
		ox_fourking: 11,
		/*** 五花牛*/
		ox_fiveking: 12,
		/*** 炸弹*/
		ox_four_same: 13,
		/*** 五小牛*/
		ox_five_min: 14,
	}

	/**获取斗牛牌型 */
	public static getCardType(cardsData: Array<any>): number {
		var result = 0;
		var numValue = 0;
		var isten = false;//扑克牌10,判断五花牛
		for (var i = 0; i < cardsData.length; i++) {
			numValue += this.getCardPoit(cardsData[i]);
			if (!isten && this.getCardValue(cardsData[i]) == 10) {
				isten = true;
			}
		}
		if (!isten && numValue == 50)//五花牛
		{
			result = this.type.ox_fiveking;
		}
		else if (this.getFiveCalves(cardsData))//五小牛
		{
			result = this.type.ox_five_min;
		}
		else if (this.getBomb(cardsData))//炸弹
		{
			result = this.type.ox_four_same;
		}
		else//牛牛及以下
		{
			for (var i = 0; i < cardsData.length; i++) {
				var temp1 = this.getCardPoit(cardsData[i]);
				for (var j = 0; j < cardsData.length; j++) {
					if (i != j) {
						var temp2 = this.getCardPoit(cardsData[j]);
						var tempValue = numValue - (temp1 + temp2);
						if (tempValue % 10 == 0) {
							result = numValue % 10 == 0 ? this.type.ox_king : numValue % 10;
							break;
						}
					}
				}
				if (result) break;
			}
		}
		return result;
	}
	/**
	 * 是否有5小牛
	 * 每张牌都小于等于5，总数小于等于10
	 * @param cardsData 需要判断的牌组
	 */
	public static getFiveCalves(cardsData: Array<any>): boolean {
		var condition = true;
		var numValue = 0;
		for (var i = 0; i < cardsData.length; i++) {
			var point = this.getCardPoit(cardsData[i])
			numValue += point;
			if (point > 5) {
				condition = false;
				break;
			}
		}
		return condition && numValue <= 10;
	}
	/**
		* 炸弹
		* 四张牌的点数相同
		* @param cardsData 需要判断的牌组
		*/
	public static getBomb(cardsData: Array<any>): boolean {
		var result = false;
		if (cardsData.length == 5) {
			var arr = [];
			for (var i = 0; i < cardsData.length; i++) {
				arr.push(Math.floor(cardsData[i] % 16));
			}
			arr.sort(function (a, b) { return a > b ? 1 : -1; });
			if ((arr[0] == arr[1] && arr[1] == arr[2] && arr[2] == arr[3]) ||
				(arr[1] == arr[2] && arr[2] == arr[3] && arr[3] == arr[4])) {
				result = true;
			}
		}
		return result;
	}
	/**
	  * 获取牌型点数
	  */
	public static getCardPoit(num: number): number {
		if (Math.floor(num % 16) >= 10) {
			return 10;
		}
		return Math.floor(num % 16);
	}
	/**
	  * 获取牌值
	  */
	public static getCardValue(cardData: number): any {
		return cardData & this.maskValue;
	}
	/** 获取花色 **/
	public static getCardColor(cardData: number): number {
		return (cardData & this.maskColor) / 16;
	}
	/**
	  * 获取牛牛牌值
	  */
	public static getNNCardValue(cbCardData, index: number): any {
		var cardValue = cbCardData[index];
		cardValue = parseInt(this.getCardValue(cardValue));
		cardValue = cardValue > 10 ? 10 : cardValue;
		return cardValue;
	}
	/** 根据数值排序，从大到小 **/
	public static SortByValue(cardData: number[]): void {
		cardData.sort((left, right) => {
			if (this.getCardValue(left) > this.getCardValue(right)) {
				return -1;
			}
			else if (this.getCardValue(left) < this.getCardValue(right)) {
				return 1;
			}
			else {
				return 0;
			}
		});
	}
	/** 根据数值排序，从大到小 **/
	public static SortByCards(cardData: number[]): number[] {
		var isTurn = false;
		for (var i = 0; i < cardData.length; i++) {
			if (isTurn) {
				break;
			}
			for (var j = 0; j < cardData.length; j++) {
				if (isTurn) {
					break;
				}
				for (var z = 0; z < cardData.length; z++) {
					if (i != j && i != z && z != j) {
						var cardValue1 = this.getNNCardValue(cardData, i);
						var cardValue2 = this.getNNCardValue(cardData, j);
						var cardValue3 = this.getNNCardValue(cardData, z);
						if ((cardValue1 + cardValue2 + cardValue3) % 10 == 0) {
							isTurn = true;
							var card1 = cardData[i];
							var card2 = cardData[j];
							var card3 = cardData[z];
							cardData.splice(cardData.indexOf(card1), 1);
							cardData.splice(cardData.indexOf(card2), 1);
							cardData.splice(cardData.indexOf(card3), 1);
							var list = [card1, card2, card3];
							this.SortByValue(list);
							this.SortByValue(cardData);
							list.push(cardData[0]);
							list.push(cardData[1]);
							cardData = list;
							break;
						}
					}
				}
			}
		}
		if (!isTurn) {
			this.SortByValue(cardData);
		}
		return cardData;
	}
	public static getMaxValue(cardData: number[]): any {
		var value = 0
		var card = 0;
		for (var i = 0; i < cardData.length; i++) {
			var cardValue = this.getCardValue(cardData[i]);
			value = value < cardValue ? cardValue : value;
			card = value < cardValue ? cardData[i] : card;
		}
		return {
			value: value,
			maskColor: this.getCardColor(card)
		};
	}
	/**判断牌1是否比牌2大**/
	public static comparisonCardTypes(cardData1: number[], cardData2: number[]): boolean {
		var cardtype1 = this.getCardType(cardData1);
		var cardtype2 = this.getCardType(cardData2);
		if (cardtype1 == cardtype2) {
			var maxValue1 = this.getMaxValue(cardData1);
			var maxValue2 = this.getMaxValue(cardData2);
			if (maxValue1.value == maxValue2.value) {
				return maxValue1.maskColor > maxValue2.maskColor;
			} else {
				return maxValue1.value > maxValue2.value;
			}
		} else {
			return cardtype1 > cardtype2;
		}
	}
}